<?php
	require_once 'init.php';
	if ( $_SERVER["REQUEST_METHOD"] == "POST" )
	{
		$userid = $_POST["userid"];
		$username = $util->quotes($_POST["username"]);
        $company = $util->quotes($_POST["company"]);
		$address = $util->quotes($_POST["address"]);
		$postcode = $util->quotes($_POST["postcode"]);
		$telephone = $util->quotes($_POST["telephone"]);        
        $mobilephone = $util->quotes($_POST["mobilephone"]);
        $position = $util->quotes($_POST["position"]);        
        $email = $util->quotes($_POST["email"]);
		
						
		if (! $adminDB->executeSQL("update ".$util->userdbName().".tbUser set fdName='".trim($username)."',fdCompany='".trim($company)."',fdPosition='".trim($position)."' where id='".$userid."'", $connID)) 
		{
			echo "<script>alert('用户更改失败！');window.loaction.href='usermanage.php';</script>";
		}
		
		//Update Address
		if (isset($address) && $address != '' && isset($postcode) && $postcode != ''){
			$addresscount = $adminDB->executeSQL("select count(*) as num from ".$util->userdbName().".tbAddress where fdUserID = '".$userid."' and fdDefault = 0", $connID);
			if($addresscount[0]['num']>0)
			{
				
				$sql = "update ".$util->userdbName().".tbAddress set fdAddress = '".trim($address)."', fdPostcode = '".trim($postcode)."' where fdUserID = '".$userid."' and fdDefault = 0";
			}
			else
			{
				
				$sql = "insert into ".$util->userdbName().".tbAddress(fdUserID, fdAddress, fdPostcode, fdDefault) values('".$userid."', '".trim($address)."', '".trim($postcode)."', 0)";
			}
			$adminDB->executeSQL($sql, $connID);
		}
		//Update Telephone
		if (isset($telephone) && $telephone != '' && $util->check_tel_regex($telephone)){
			$telecount = $adminDB->executeSQL("select count(*) as num from ".$util->userdbName().".tbPhone where fdUserID = '".$userid."' and fdTypeID = 0 and fdDefault = 0", $connID);
			if($telecount[0]['num']>0)
			{
				$sql = "update ".$util->userdbName().".tbPhone set fdPhone = '".trim($telephone)."' where fdUserID = '".$userid."' and fdTypeID = 0 and fdDefault = 0";
			}
			else
			{
				$sql = "insert into ".$util->userdbName().".tbPhone(fdUserID, fdPhone, fdDefault,fdTypeID) values('".$userid."', '".trim($telephone)."', 0,0)";
			}			
			$adminDB->executeSQL($sql, $connID);
		}
		else 
		{
			echo "<script>alert('电话号码更改失败！请确认已正确填写！');window.loaction.href='usermanage.php';</script>";
		}
		//Update Mobilephone
		if (isset($mobilephone) && $mobilephone != ''&& $util->check_phone_regex($mobilephone)){
			$mobilecount = $adminDB->executeSQL("select count(*) as num from ".$util->userdbName().".tbPhone where fdUserID = '".$userid."' and fdTypeID = 1 and fdDefault = 0", $connID);
			if($mobilecount[0]['num']>0)
			{
				$sql = "update ".$util->userdbName().".tbPhone set fdPhone = '".trim($mobilephone)."' where fdUserID = '".$userid."' and fdTypeID = 1 and fdDefault = 0";
			}
			else
			{
				$sql = "insert into ".$util->userdbName().".tbPhone(fdUserID, fdPhone, fdDefault,fdTypeID) values('".$userid."', '".trim($mobilephone)."', 0,1)";
			}			
			$adminDB->executeSQL($sql, $connID);
		}
		else 
		{
			echo "<script>alert('手机号码更改失败！请确认已正确填写！');window.loaction.href='usermanage.php';</script>";
		}
		//Update Email
		if (isset($email) && $email != '' && $util->check_email_regex($email)){
			$emailcount = $adminDB->executeSQL("select count(*) as num from ".$util->userdbName().".tbEmail where fdUserID = '".$userid."' and fdDefault = 0", $connID);
			if($emailcount[0]['num']>0)
			{
				$sql = "update ".$util->userdbName().".tbEmail set fdEmail = '".trim($email)."' where fdUserID = '".$userid."' and fdDefault = 0";
			}
			else
			{
				$sql = "insert into ".$util->userdbName().".tbEmail(fdUserID, fdEmail, fdDefault) values('".$userid."', '".trim($email)."', 0)";
			}
			$adminDB->executeSQL($sql, $connID);			
		}		
		else 
		{
			echo "<script>alert('电子邮件更改失败！请确认已正确填写！');window.loaction.href='usermanage.php';</script>";
		}
		
		echo "<script>alert('用户更改成功！');window.location.href='usercenter.php';</script>";			
	}
?>